from flask import session, current_app, g
from info.models import User


# 过滤器
def guolvqi(index):
    if index == 1:
        return 'first'
    elif index == 2:
        return 'second'
    elif index == 3:
        return 'third'
    else:
        return ''


# 自定义装饰器,检查用户登录状态
def login_required(fun):
    def inner(*args, **kwargs):
        user_id = session.get('user_id')
        user = None
        if user_id:
            try:
                user = User.query.filter_by(id=user_id).first()
            except Exception as e:
                current_app.logger.error(e)
        g.user = user
        return fun(*args, **kwargs)
    inner.__name__ = fun.__name__
    """
    或者在inner前加上＠functools.wraps(fun)----------------->优先使用
    """
    return inner